﻿using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Publishing.WebControls;
using System.Web.UI.WebControls;
using System;

namespace lcqwp
{
    /// <summary>
    /// The tool part used to customize the choice of header and item XSL files
    /// used by the LocalizableContentByQueryWebPart.
    /// </summary>
    [CLSCompliant(false)]
    public class LocalizableContentByQueryWebPartToolPart : ToolPart
    {
        private const string LineBreak = "<br/>";
        private TextBox _txtItemStyleUrl;
        private AssetUrlSelector _itemStyleUrlPicker;

        private TextBox _txtHeaderStyleUrl;
        private AssetUrlSelector _headerStyleUrlPicker;

        private TextBox _txtMainStyleUrl;
        private AssetUrlSelector _mainStyleUrlPicker;

        private TextBox _txtResourceFilename;
        private TextBox _txtExtraFields;

        private LocalizableContentByQueryWebPart _webPart;

        private LocalizableContentByQueryWebPart WebPart
        {
            get
            {
                if (_webPart == null)
                    _webPart = ParentToolPane.SelectedWebPart as LocalizableContentByQueryWebPart;
                return _webPart;
            }
        }

        private void CreateAnonymousLabel(string text)
        {
            Label label = new Label { Text = text };
            Controls.Add(label);
        }

        private void InsertLineBreak()
        {
            InsertLineBreak(1);
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls 
        /// that use composition-based implementation to create any child controls 
        /// they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Title = "UI Customization";

            CreateResourceFilenameTextBox();
            InsertLineBreak(2);
            CreateExtraFieldsTextBox();
            InsertLineBreak(2);
            CreateItemXslControls();
            InsertLineBreak(2);
            CreateHeaderXslControls();
            InsertLineBreak(2);
            CreateMainXslControls();
        }

        private void CreateExtraFieldsTextBox()
        {
            CreateAnonymousLabel("Extra Fields (Comma separated)");
            InsertLineBreak();
            _txtExtraFields = new TextBox() { ID = "txtExtraFields", Text = WebPart.ExtraFields };
            Controls.Add(_txtExtraFields);
        }

        private void CreateResourceFilenameTextBox()
        {
            CreateAnonymousLabel("Resource Filename (\"Resources.&lt;filename&gt;\")");
            InsertLineBreak();
            _txtResourceFilename = new TextBox() { ID = "txtResourceFilename", Text = WebPart.ResourceFilename };
            Controls.Add(_txtResourceFilename);
        }

        private void CreateHeaderXslControls()
        {
            CreateAnonymousLabel("Xsl Header Style");
            InsertLineBreak();

            _txtHeaderStyleUrl = new TextBox { ID = "txtHeaderUrl", Text = WebPart.HeaderXslLink };
            Controls.Add(_txtHeaderStyleUrl);

            _headerStyleUrlPicker = new AssetUrlSelector
            {
                ID = "pckHeaderUrl",
                AssetUrlClientID = _txtHeaderStyleUrl.ClientID,
                AssetUrlTextBoxVisible = false,
                AssetUrl = WebPart.HeaderXslLink
            };
            Controls.Add(_headerStyleUrlPicker);
        }

        /// <summary>
        /// When overridden in a derived class, this method is called after
        ///  all other tool parts have called their ApplyChanges methods.
        /// </summary>
        public override void SyncChanges()
        {
            base.SyncChanges();

            _itemStyleUrlPicker.AssetUrl = WebPart.ItemXslLink;
            _txtItemStyleUrl.Text = WebPart.ItemXslLink;

            _headerStyleUrlPicker.AssetUrl = WebPart.HeaderXslLink;
            _txtHeaderStyleUrl.Text = WebPart.HeaderXslLink;

            _mainStyleUrlPicker.AssetUrl = WebPart.MainXslLink;
            _txtMainStyleUrl.Text = WebPart.MainXslLink;

            _txtExtraFields.Text = WebPart.ExtraFields;
            _txtResourceFilename.Text = WebPart.ResourceFilename;

        }

        private void CreateMainXslControls()
        {
            CreateAnonymousLabel("Xsl Main Style");
            InsertLineBreak();

            _txtMainStyleUrl = new TextBox { ID = "txtMainUrl", Text = WebPart.MainXslLink };
            Controls.Add(_txtMainStyleUrl);

            _mainStyleUrlPicker = new AssetUrlSelector
            {
                ID = "pckMainUrl",
                AssetUrlClientID = _txtMainStyleUrl.ClientID,
                AssetUrlTextBoxVisible = false,
                AssetUrl = WebPart.MainXslLink
            };
            Controls.Add(_mainStyleUrlPicker);
        }

        /// <summary>
        /// Called when the user clicks the OK or the Apply button in the tool pane.
        /// </summary>
        public override void ApplyChanges()
        {
            base.ApplyChanges();
            WebPart.ItemXslLink = _txtItemStyleUrl.Text;
            WebPart.HeaderXslLink = _txtHeaderStyleUrl.Text;
            WebPart.MainXslLink = _txtMainStyleUrl.Text;
            WebPart.ResourceFilename = _txtResourceFilename.Text;
            WebPart.ExtraFields = _txtExtraFields.Text;
        }

        private void InsertLineBreak(int count)
        {
            for (int i = 0; i < count; i++)
            {
                LiteralControl lit = new LiteralControl(LineBreak);
                Controls.Add(lit);
            }
        }

        private void CreateItemXslControls()
        {
            CreateAnonymousLabel("Xsl Item Style");
            InsertLineBreak();

            _txtItemStyleUrl = new TextBox { ID = "txtItemUrl", Text = WebPart.ItemXslLink };
            Controls.Add(_txtItemStyleUrl);

            _itemStyleUrlPicker = new AssetUrlSelector
            {
                ID = "pckItemUrl",
                AssetUrlClientID = _txtItemStyleUrl.ClientID,
                AssetUrlTextBoxVisible = false,
                AssetUrl = WebPart.ItemXslLink
            };
            Controls.Add(_itemStyleUrlPicker);
        }
    }
}
